/**
 * lilang
 * OnlineAction.java
 * 2013-6-24下午1:03:48
 */
package com.cq.oa.common.util.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oa.base.util.FileUtils;
import oa.base.web.Action.BaseAction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.cq.oa.common.utils.Convert2swf;
import com.cq.oa.common.utils.ToolUtils;
import com.cq.oa.domain.sm.User;

/**
 * @author lilang
 *
 */
@SuppressWarnings("serial")
public class OnLineAction extends BaseAction<User>{
	
	private static final Log logger = LogFactory.getLog(OnLineAction.class);
	/* (non-Javadoc)
	 * @see oa.base.web.Action.BaseAction#getEntityClass()
	 */
	@Override
	protected Class<User> getEntityClass() {
		return User.class;
	}
	
	
	public String online(){
		return "online";
	}
	
	public String swfView() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String filepath = FileUtils.getLocalUrl(getUrl());
		File soureFile = new File(filepath);
		File swfFile = new File(filepath + ToolUtils.SWF);
		if (filepath.endsWith(ToolUtils.SWF)) {
			swfFile = new File(filepath);
		}
		if (!swfFile.exists() && soureFile.exists() && !filepath.endsWith(ToolUtils.SWF)) {
			Convert2swf.office2swf(soureFile.getPath());
		}
		OutputStream outputstream = null;
		BufferedInputStream input = null;
		try {
			if (swfFile.exists()) {
				String agent = request.getHeader("USER-AGENT");
				// File file=new File(FilePathUtils.getFullPath(entity.getUrl()));
				String outFileName = swfFile.getName();
				if ((agent != null) && (-1 != agent.indexOf("MSIE")))
					outFileName = URLEncoder.encode(outFileName, "UTF-8");
				else if ((agent != null) && (-1 != agent.indexOf("Mozilla"))) {
					outFileName = MimeUtility.encodeText(outFileName, "UTF8", "B");
				} else {
					outFileName = URLEncoder.encode(outFileName, "UTF-8");
				}
				response.setHeader("Cache-Control", "private, max-age=10800, pre-check=10800");
				response.setHeader("Pragma", "private");
				SimpleDateFormat format = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
				response.setHeader("Expires", format.format(new Date()));
				response.setContentType("application/x-shockwave-flash");
				response.setContentLength(Integer.valueOf(String.valueOf(swfFile.length())));
				response.setHeader("Content-disposition", "attachment;filename=\"" + outFileName + "\"");
				response.setHeader("Accept-Ranges", "bytes");
				outputstream = response.getOutputStream();

				input = new BufferedInputStream(new java.io.FileInputStream(swfFile));
				byte[] buffer = new byte[4096];
				int len = 0;
				while ((len = input.read(buffer)) > 0) {
					outputstream.write(buffer, 0, len);
					outputstream.flush();
				}
			}
		} catch (Exception e) {
			logger.error(e.getStackTrace());
		} finally {
			// 关闭输入流
			if (null!=input) {
				try {
					input.close();
				} catch (IOException e) {
					logger.error(e.getStackTrace());
				}
			}
			// 关闭输出流
			if (null!=outputstream) {
				try {
					outputstream.flush();
					outputstream.close();
				} catch (IOException e) {
					logger.error(e.getStackTrace());
				}
			}
		}
		return JSON;
	}
	
	

}
